package com.jerry.mingxing.ui.detail;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jerry.mingxing.R;
import com.jerry.mingxing.bean.DetailBean;
import com.jerry.mingxing.bean.ShowBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/4.
 */
public class DetailAdapter extends BaseAdapter {
    private List<DetailBean.DataBean.ItemsBean> totallist;
    private Context context;
    private LayoutInflater inflater;
    private static final int TYPE_1 = 0, TYPE_2 = 1;

    public DetailAdapter(List<DetailBean.DataBean.ItemsBean> totallist, Context context) {
        this.totallist = totallist;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return totallist.size();
    }

    @Override
    public Object getItem(int position) {
        return totallist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (totallist.get(position).getCells().size() > 1) {
            return TYPE_1;
        }
        return TYPE_2;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        switch (type) {
            case TYPE_1:
                ViewHolder1 mholder1=null;
                if (convertView==null){

                    convertView = inflater.inflate(R.layout.detail_item1, parent, false);
                    mholder1=new ViewHolder1(convertView);
                    convertView.setTag(mholder1);
                }else {
                    mholder1= (ViewHolder1) convertView.getTag();
                }
                mholder1.detailItem1Left.setImageURI(Uri.parse(totallist.get(position).getCells().get(0).getComponent().getPicUrl()));
                mholder1.detailItem1Right.setImageURI(Uri.parse(totallist.get(position).getCells().get(1).getComponent().getPicUrl()));

                break;
            case TYPE_2:
                ViewHolder2 mholder2=null;
                if (convertView==null){

                    convertView = inflater.inflate(R.layout.detail_item2, parent, false);
                    mholder2=new ViewHolder2(convertView);
                    convertView.setTag(mholder2);
                }else {
                    mholder2= (ViewHolder2) convertView.getTag();
                }
                mholder2.detailItem2.setImageURI(Uri.parse(totallist.get(position).getCells().get(0).getComponent().getPicUrl()));

                break;


        }

        return convertView;
    }



    static class ViewHolder1 {
        @Bind(R.id.detail_item1_left)
        SimpleDraweeView detailItem1Left;
        @Bind(R.id.detail_item1_right)
        SimpleDraweeView detailItem1Right;

        ViewHolder1(View view) {
            ButterKnife.bind(this, view);
        }
    }

    class ViewHolder2 {
        @Bind(R.id.detail_item2)
        SimpleDraweeView detailItem2;

        ViewHolder2(View view) {
            ButterKnife.bind(this, view);
        }
    }
    public void reloadData(List list) {

        totallist=list;
        notifyDataSetChanged();
    }
}
